package com.example.web.office_specifications.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.web.office_specifications.entity.OfficeSpecifications;
import com.example.web.office_specifications.mapper.OfficeSpecificationsMapper;
import com.example.web.office_specifications.service.IOfficeSpecificationsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LS
 * @since 2024-05-16
 */
@Service
public class OfficeSpecificationsServiceImpl extends ServiceImpl<OfficeSpecificationsMapper, OfficeSpecifications> implements IOfficeSpecificationsService {

    @Autowired
    private OfficeSpecificationsMapper officeSpecificationsMapper;
    List<Long> ids = new ArrayList<>();

    @Override
    public boolean deleteNode(Long id) {

        ids.clear();
        List<Long> ids = getDeleteIds(id);
        officeSpecificationsMapper.deleteBatchIds(ids);
        return true;
    }

    private List<Long> getDeleteIds(Long id) {

        try {
            ids.add(id);
            QueryWrapper<OfficeSpecifications> query = new QueryWrapper<>();
            query.lambda().eq(OfficeSpecifications::getParentId, id);
            List<OfficeSpecifications> child = officeSpecificationsMapper.selectList(query);
            if (child != null) {
                child.forEach(item -> {
                    getDeleteIds(item.getId());
                });
            }
            return ids;
        } catch (Exception ex) {
            System.out.println("getDeleteIds Error:" + ex.getMessage());
            return null;
        }
    }

}
